/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.serverpackets.S_NpcChatPacket;

public class L1NpcChatTimer extends TimerTask {
	private static final Logger _log = Logger.getLogger(L1NpcChatTimer.class
			.getName());

	private final L1NpcInstance _npc;

	private final String _chatId;

	private final boolean _isShout;

	private final int _appearanceOrDead;

	public L1NpcChatTimer(L1NpcInstance npc, String chatId, boolean isShout,
			int appearanceOrDead) {
		_npc = npc;
		_chatId = chatId;
		_isShout = isShout;
		_appearanceOrDead = appearanceOrDead;
	}

	@Override
	public void run() {
		try {
			if (_npc == null) {
				return;
			}
			if (_npc.getHiddenStatus() != L1NpcInstance.HIDDEN_STATUS_NONE
					|| _npc._destroyed) {
				_npc.setChatId(null);
				return;
			}
			if (_appearanceOrDead == L1NpcInstance.NPC_CHAT_APPEARANCE
					&& _npc.isDead()) {
				_npc.setChatId(null);
				return;
			}

			if (!_isShout) {
				_npc.broadcastPacket(new S_NpcChatPacket(_npc, _chatId, 0));
			} else {
				_npc.wideBroadcastPacket(new S_NpcChatPacket(_npc, _chatId, 2));
			}

		} catch (Throwable e) {
			_log.log(Level.WARNING, e.getLocalizedMessage(), e);
		}
	}

}
